cmake_minimum_required(VERSION 3.20)
project(ros2_mqtt_bridge)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 1. 查找依赖
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
#find_package(launch_ros REQUIRED)  # Launch 依赖
find_package(PkgConfig REQUIRED)

# 查找 MQTT 库（libmosquitto）
pkg_check_modules(MOSQUITTO REQUIRED libmosquitto)

# 2. 编译节点（mqtt_bridge_node）
add_executable(mqtt_bridge_node src/mqtt_bridge.cpp)
target_include_directories(mqtt_bridge_node PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
  ${MOSQUITTO_INCLUDE_DIRS}  # MQTT 头文件路径
)
target_link_libraries(mqtt_bridge_node ${MOSQUITTO_LIBRARIES})  # 链接 MQTT 库
ament_target_dependencies(
  mqtt_bridge_node
  "rclcpp"
  "std_msgs"
)

# 3. 安装节点和 Launch 文件
# 安装节点到 lib/${PROJECT_NAME}
install(TARGETS
  mqtt_bridge_node
  DESTINATION lib/${PROJECT_NAME}
)

# 安装 Launch 文件到 share/${PROJECT_NAME}/launch（关键步骤）
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}
)

# 安装配置文件（如果有 config 文件夹，需添加此步骤）
install(DIRECTORY
  config
  DESTINATION share/${PROJECT_NAME}
)

# 4. 测试配置
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  set(ament_cmake_copyright_FOUND TRUE)
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()